package com.tim.third.utils;

import com.tim.ArgConstant;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.util.*;
import org.springframework.util.comparator.Comparators;

import java.io.File;
import java.io.InputStream;
import java.io.Writer;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.URISyntaxException;
import java.nio.file.Paths;
import java.util.*;

/**
 * @Description:
 * @Version 1.0, 2021/6/10
 * @Author Tim
 */
public class SpringBootUtils extends ArgConstant {
	public static void main(String[] args) throws URISyntaxException {
		// id生成器
		System.out.println(new AlternativeJdkIdGenerator().generateId());
		// 类、包工具
		System.out.println(ClassUtils.convertClassNameToResourcePath(SpringBootUtils.class.getName()));
		System.out.println(ResourceUtils.toURI("https://www.cnblogs.com/theRhyme/p/11057233.html"));
		String str = System.getProperties().toString();
		System.out.println(str);
		System.out.println(SystemPropertyUtils.resolvePlaceholders("a=1, b=${java.runtime.name}", true));

	}

	public static void utils() throws Exception {
	    // AOP工具类
        AopUtils.isAopProxy(null);
		// id生成器
		new AlternativeJdkIdGenerator().generateId();
		// 路径匹配；?单字符，*多字符，**多级目录
		AntPathMatcher antPathMatcher;
		// 断言
		Assert.hasLength("", "");
		// 集合工具
		CollectionUtils.isEmpty(Collections.emptyMap());
		// 加密
		DigestUtils.md5(new byte[10]);
		// 文件复制
		FileCopyUtils.copy((File) null, (File) null);
		// 文件/文件夹复制、删除
		FileSystemUtils.copyRecursively((File) null, (File) null);
		// 对象工具；判空、转字符串等
		ObjectUtils.isEmpty(null);
		// 占位符替换
		new PropertyPlaceholderHelper(S, S).replacePlaceholders("", (Properties) null);
		// 反射工具
		ReflectionUtils.findMethod(null, null);
		// 资源工具
		ResourceUtils.getFile(S);
		// 解析${}，必须为System.getProperties
		SystemPropertyUtils.resolvePlaceholders("");
		// 比较器
        Comparator comparator = Comparators.comparable();
        Comparator.naturalOrder().reversed();// jdk比较器
	}

	public static void otherUtil(){
		BeanUtils.copyProperties(null, null);// src, dest


	}

	
	public static void alternativeJdkIdGenerator() {
		System.out.println(new AlternativeJdkIdGenerator().generateId().toString());
		System.out.println(UUID.randomUUID().toString());

	}

	
	public static void antPathMatcher() {
		AntPathMatcher antPathMatcher=new AntPathMatcher();
		// 是否模式；?,*,**
		System.out.println(antPathMatcher.isPattern("/adb/*"));
		System.out.println(antPathMatcher.isPattern("/adb/{id}"));
		// 匹配；path完全匹配pattern，path匹配部分pattern
		System.out.println(antPathMatcher.match("/user/*/*/*", "/user/age/cc"));
		System.out.println(antPathMatcher.matchStart("/user/*/*/*", "/user/age/cc"));
		System.out.println(antPathMatcher.matchStart("/user/{id}", "/user/1"));
		System.out.println(antPathMatcher.matchStart("/user/id", "/user/1"));
		// 获取匹配部分
		System.out.println(antPathMatcher.extractPathWithinPattern("/user/*/*/*", "/user/age/cc"));
		System.out.println(antPathMatcher.extractPathWithinPattern("/user/**", "/user/age/cc"));
		System.out.println(antPathMatcher.extractPathWithinPattern("/user/a*/c?", "/user/age/cc"));
		// 获取pathVarable，{name[:reg]}，reg为正则表达式
		System.out.println(antPathMatcher.extractUriTemplateVariables("/user/a*/c?", "/user/age/cc"));
		System.out.println(antPathMatcher.extractUriTemplateVariables("/user/{id}", "/user/1"));
		System.out.println(antPathMatcher.extractUriTemplateVariables("/user/{ID:\\d+}", "/user/1"));


	}

	
	public static void assertTest() {
		// true、空判定
		Assert.isTrue(true, "not true err msg");
		Assert.isNull(null, "err msg");
		Assert.notNull(1L, "err msg");
		// 空串、空白串、不包含
		Assert.hasLength(" ", "err msg");
		Assert.hasText("123", "err msg");
		Assert.doesNotContain("src", "searchStr", "err msg");
		// 集合判空、空元素
		Assert.notEmpty(Arrays.asList(""), "err msg");
		Assert.noNullElements(ArrayUtils.EMPTY_OBJECT_ARRAY, "err msg");
		// 是实例；是子类
		Assert.isInstanceOf(String.class, "", "err msg");
		Assert.isAssignable(List.class, ArrayList.class, "err msg");

		System.out.println("success");
	}

	
	public static void collectionUtils() {
		CollectionUtils.isEmpty(Collections.emptyMap());

		CollectionUtils.toMultiValueMap(null);
	}

	
	public static void digestUtils() {
		// md5加密
		org.springframework.util.DigestUtils.md5Digest(new byte[10]);
	}

	
	public static void fileCopyUtils() throws Exception {
		FileCopyUtils.copy((File) null, (File) null);

		FileCopyUtils.copy("", (Writer)null);
	}

	
	public static void fileSystemUtils() throws Exception {
		// 复制
		FileSystemUtils.copyRecursively((File) null, (File) null);
		// 删除
		FileSystemUtils.deleteRecursively(Paths.get(""));
	}

	
	public static void objectUtils() {
		ObjectUtils.isEmpty(null);
	}

	
	public static void propertyPlaceholderHelper() {
		new PropertyPlaceholderHelper(S, S).replacePlaceholders("", (Properties) null);
	}

	
	public static void reflectionUtils() {
		// 属性
		Field f=ReflectionUtils.findField(String.class, "value");
		ReflectionUtils.makeAccessible(f);
		ReflectionUtils.getField(f, "");
		// 方法
		Method m=ReflectionUtils.findMethod(String.class, "length");
		ReflectionUtils.makeAccessible(m);
		ReflectionUtils.invokeMethod(m, "");


	}

	
	public static void resourceUtils() throws Exception {
		ResourceUtils.getFile(S);
		ResourceUtils.toURI("");
	}

	
	public static void streamUtils() throws Exception {
		StreamUtils.copyToString((InputStream) null, null);
	}

	
	public static void systemPropertyUtils() {
		SystemPropertyUtils.resolvePlaceholders("");
	}

	
	public static void comparators() throws Exception {
		Comparator<Integer> comparator = Comparators.<Integer>comparable();
		Comparators.nullsHigh(comparator);
	}

	
	public static void beanUtils() throws Exception {
		// 复制
		BeanUtils.copyProperties(null, null);

		// 获取反射元素
		BeanUtils.findPrimaryConstructor(HashMap.class);
		BeanUtils.findMethod(HashMap.class, "get", Object.class);
		BeanUtils.getPropertyDescriptor(Date.class, "time");

	}
}
